package com.gd.data.platform.mapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gd.data.platform.entity.ApiCallLog;

/**
 * API调用日志Mapper
 * 操作ClickHouse中的API调用日志数据
 * 
 * @author gd-data-platform
 * @since 1.0.0
 */
@Mapper
public interface ApiCallLogMapper extends BaseMapper<ApiCallLog> {

    /**
     * 统计指定时间窗口内的调用量
     */
    @Select("SELECT COUNT(*) as call_count FROM api_call_logs " +
            "WHERE request_time >= #{startTime} AND request_time <= #{endTime}")
    Long countCallsByTimeRange(@Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 统计指定时间窗口内的成功调用量
     */
    @Select("SELECT COUNT(*) as success_count FROM api_call_logs " +
            "WHERE request_time >= #{startTime} AND request_time <= #{endTime} AND success = true")
    Long countSuccessCallsByTimeRange(@Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 统计指定时间窗口内的失败调用量
     */
    @Select("SELECT COUNT(*) as failure_count FROM api_call_logs " +
            "WHERE request_time >= #{startTime} AND request_time <= #{endTime} AND success = false")
    Long countFailureCallsByTimeRange(@Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 统计指定IP的调用量
     */
    @Select("SELECT COUNT(*) as call_count FROM api_call_logs " +
            "WHERE client_ip = #{clientIp} AND request_time >= #{startTime} AND request_time <= #{endTime}")
    Long countCallsByIp(@Param("clientIp") String clientIp,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 统计指定接口的调用量
     */
    @Select("SELECT COUNT(*) as call_count FROM api_call_logs " +
            "WHERE api_name = #{apiName} AND request_time >= #{startTime} AND request_time <= #{endTime}")
    Long countCallsByApi(@Param("apiName") String apiName,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);

    /**
     * 获取接口调用排行
     */
    @Select("SELECT api_name, COUNT(*) as call_count, " +
            "SUM(CASE WHEN success = true THEN 1 ELSE 0 END) * 100.0 / COUNT(*) as success_rate " +
            "FROM api_call_logs " +
            "WHERE request_time >= #{startTime} AND request_time <= #{endTime} " +
            "GROUP BY api_name " +
            "ORDER BY call_count DESC " +
            "LIMIT #{limit}")
    List<Map<String, Object>> getApiCallRankings(@Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            @Param("limit") Integer limit);

    /**
     * 获取IP访问排行
     */
    @Select("SELECT client_ip, COUNT(*) as access_count " +
            "FROM api_call_logs " +
            "WHERE request_time >= #{startTime} AND request_time <= #{endTime} " +
            "GROUP BY client_ip " +
            "ORDER BY access_count DESC " +
            "LIMIT #{limit}")
    List<Map<String, Object>> getIpAccessRankings(@Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            @Param("limit") Integer limit);

    /**
     * 计算平均响应时间
     */
    @Select("SELECT AVG(response_time_ms) as avg_response_time " +
            "FROM api_call_logs " +
            "WHERE request_time >= #{startTime} AND request_time <= #{endTime} AND success = true")
    Double getAvgResponseTime(@Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime);
}
